package com.saishi.servlet;

import com.google.gson.JsonObject;
import com.saishi.dao.ExamDao;
import com.saishi.dao.QuestionDao;
import com.saishi.dao.impl.ExamDaoImpl;
import com.saishi.dao.impl.QuestionDaoImpl;
import com.saishi.entity.Question;
//import com.sun.deploy.util.StringUtils;
//import com.sun.imageio.plugins.common.I18N;
import org.apache.commons.lang3.StringUtils;
//import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
//import java.util.HashMap;
//import java.util.Map;

@WebServlet("/sjbx/*")
public class ShiJuanbxServlet extends BaseServlet {

    protected void addtimu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String tg = request.getParameter("tg");
        String fz = request.getParameter("fz");
        String zsd = request.getParameter("zsd");
        String tx = request.getParameter("tx");
//        int zsdi = 0;
//        if (zsd.equals("Java高级")||zsd.equals("java高级")){
//            zsdi = 1;
//        }else if (zsd.equals("MySQL")||zsd.equals("mysql")){
//            zsdi = 2;
//        }else if (zsd.equals("HTML5")||zsd.equals("html5")){
//            zsdi = 3;
//        }else if (zsd.equals("JAVAWeb")||zsd.equals("javaweb")){
//            zsdi = 4;
//        }else {
//            zsdi = 0;
//        }
//        System.out.println("知识点"+zsdi);

        String zqda = request.getParameter("zqda");
        String A = request.getParameter("a");
        String B = request.getParameter("b");
        String C = request.getParameter("c");
        String D = request.getParameter("d");

        JsonObject jsonObject=new  JsonObject();
        jsonObject.addProperty("A",A);
        jsonObject.addProperty("B",B);
        jsonObject.addProperty("C",C);
        jsonObject.addProperty("D",D);
//json对象转换成json字符串
        String jsonStr=jsonObject.toString();

//        System.out.println(jsonStr);
       //    String S = "{'A':A,'B':B,'C':C,'D':D}";

        QuestionDao qudao = new QuestionDaoImpl();
        Question question = null;
        if (A==null){
           session.setAttribute("tjtg",tg);
           session.setAttribute("tjzsd",zsd);
           session.setAttribute("tjfz",fz);
           session.setAttribute("tjtx",tx);
           session.setAttribute("tjzqda",zqda);
           question = new Question(tg,Integer.parseInt(fz),Integer.parseInt(tx),zqda);
       }else {
           session.setAttribute("tjtg",tg);
           session.setAttribute("tjzsd",zsd);
           session.setAttribute("tjfz",fz);
            session.setAttribute("tjtx",tx);
           session.setAttribute("tjzqda",zqda);
           session.setAttribute("tjxx",jsonStr);

            question = new Question(tg,Integer.parseInt(fz),Integer.parseInt(tx),jsonStr,zqda);
       }
        System.out.println("添加题的信息"+question);
       //添加到题库表，和修改试卷表的题目id字段。再跳转到sjbx.jsp页面。  *******进入sjbx.jsp页面的时候要先查询表里有多少个题目再到页面以循环div显示出来*******
        //添加到题库
        int ok = 0;//记得改回0
        try {
            ok = qudao.insert(question);
            System.out.println(ok+"是否添加成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        int quId = 0 ;
        if (ok!=0){
            try {
                System.out.println("123");
                quId = qudao.seleQuesId(tg);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        ExamDao examDao = new ExamDaoImpl();

        int id = Integer.parseInt(session.getAttribute("examid").toString());   // 从试卷基础信息表获取到试卷id
//        int id =1;
        if (quId!=0){
            String quesits = null;
            try {
               quesits = examDao.examque(id);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            System.out.println("试卷表题目"+quesits);
            if (quesits!=null&&quesits.length()>=1){
                ArrayList arr =new ArrayList(Arrays.asList(quesits));
                System.out.println("取出来的"+arr);
                arr.add(String.valueOf(quId));
                System.out.println("添加了新题后的"+arr);
                String str= StringUtils.join(arr,",");
                System.out.println("转成String后的"+str);
                try {
                   int o = examDao.update(str,id);
                    System.out.println("o"+o);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }else {
                //没题的话直接加就行了
                String str= String.valueOf(quId);
                System.out.println("int转成String后的str:"+str);
                try {
                    int o = examDao.update(str,id);
                    System.out.println("o"+o);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            seletm(request,response);

        }


    }


    protected void seletm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ExamDao examDao = new ExamDaoImpl();
        QuestionDao quDao = new QuestionDaoImpl();
        HttpSession session = request.getSession();
        String quesits = null;
        int id = Integer.parseInt(session.getAttribute("examid").toString());
//        int id =1;
        try {
            quesits = examDao.examque(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("试卷表题目"+quesits);
        ArrayList arr =new ArrayList(Arrays.asList(quesits.split(",")));
        ArrayList<Question> list = new ArrayList<Question>();
        for( Object i: arr ){
            System.out.println(i);
//            通过id查出题目，把题目以对象放道Arraylest里
            int i1 = Integer.parseInt(i.toString());
            try {
                Question question = quDao.seleQuery(i1);
                list.add(question);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        System.out.println(list+"=========");
        session.setAttribute("tmlist",list);
        response.sendRedirect("/sjbx.jsp");
    }
    protected void deletm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int tmid = Integer.parseInt(request.getParameter("tmid").toString());
        HttpSession session = request.getSession();
//        System.out.println(tmid+"tmid");
        ExamDao examDao = new ExamDaoImpl();
                int id = Integer.parseInt(session.getAttribute("examid").toString());
//        int id =1;
            String quesits = null;
            try {
                quesits = examDao.examque(id);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            System.out.println("试卷表题目"+quesits);

            if (quesits!=null&&quesits.length()>=1) {
                ArrayList arr = new ArrayList(Arrays.asList(quesits.split(",")));
                ArrayList arr1 = new ArrayList();
                System.out.println("取出来的arr" + arr);

                for( Object i: arr ){
//                    System.out.println(i+"===");
                    int i1 = Integer.parseInt(i.toString());

                    if (i1!=tmid){
                        arr1.add(i1);
                    }
                }
                System.out.println(arr1+"===="+arr);

                String str = StringUtils.join(arr1, ",");
                System.out.println("转成String后的arr1" + str);
                try {
                    int o = examDao.update(str, id);
                    System.out.println("是否修改成功" + o);
                    seletm(request,response);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
    }
}
